package com.cain.DontTapTheWhiteTile.activity;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cain.DontTapTheWhiteTile.R;
import com.cain.DontTapTheWhiteTile.tool.Constants;
import com.cain.DontTapTheWhiteTile.tool.MyNumber;
import com.cain.DontTapTheWhiteTile.tool.MyString;

public class ResultActivity extends AppCompatActivity {

    private static final String TAG = "ResultActivity";

    public static final int RESULT_RESTART = 0;   //重新开始
    public static final int RESULT_MAIN = 1;   //返回主菜单

    private RelativeLayout panel;
    private TextView modeTextView;
    private TextView titleTextView;
    private TextView scoreTextView;
    private TextView historyTextView;
    private Button backButton;
    private Button resetButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String mode = intent.getStringExtra("mode");
        long history = intent.getLongExtra("history", 0);
        long score = intent.getLongExtra("score", 0);
        int state = intent.getIntExtra("state", Constants.GAME_OVER_NORMAL);

        setContentView(R.layout.activity_result);
        panel = (RelativeLayout) findViewById(R.id.result_panel);
        modeTextView = (TextView) findViewById(R.id.result_mode);
        titleTextView = (TextView) findViewById(R.id.result_title);
        scoreTextView = (TextView) findViewById(R.id.result_score);
        historyTextView = (TextView) findViewById(R.id.result_history);
        backButton = (Button) findViewById(R.id.result_back);
        resetButton = (Button) findViewById(R.id.result_reset);

        if (state == Constants.GAME_OVER_WRONG) {
            panel.setBackgroundColor(Constants.BLOCK_RED);
        }
        if (mode.equals(Constants.BEST_CLASSIC_EASY_FEW)) {
            if (state == Constants.GAME_OVER_NORMAL) {
                panel.setBackgroundColor(Constants.BLOCK_GREEN);
                scoreTextView.setText(MyString.timeFormatToString(MyNumber.millisToFormat(score)));
            } else {
                scoreTextView.setText("败了!");
            }
            modeTextView.setText("25");
            titleTextView.setText("经典模式");
            if ((history == 0 || score < history) && state == Constants.GAME_OVER_NORMAL) {
                historyTextView.setText("新纪录");
            } else {
                historyTextView.setText("历史最佳 " + MyString.timeFormatToString(MyNumber.millisToFormat(history)));
            }
        } else if (mode.equals(Constants.BEST_CLASSIC_EASY_MUCH)) {
            if (state == Constants.GAME_OVER_NORMAL) {
                panel.setBackgroundColor(Constants.BLOCK_GREEN);
                scoreTextView.setText(MyString.timeFormatToString(MyNumber.millisToFormat(score)));
            } else {
                scoreTextView.setText("败了!");
            }
            modeTextView.setText("50");
            titleTextView.setText("经典模式");
            if ((history == 0 || score < history) && state == Constants.GAME_OVER_NORMAL) {
                historyTextView.setText("新纪录");
            } else {
                historyTextView.setText("历史最佳 " + MyString.timeFormatToString(MyNumber.millisToFormat(history)));
            }
        } else if (mode.equals(Constants.BEST_CLASSIC_NORMAL_FEW) || mode.equals(Constants.BEST_CLASSIC_NORMAL_MUCH)) {
            if (state == Constants.GAME_OVER_NORMAL) {
                panel.setBackgroundColor(Constants.BLOCK_GREEN);
                scoreTextView.setText(MyString.timeFormatToString(MyNumber.millisToFormat(score)));
            } else {
                scoreTextView.setText("败了!");
            }
            modeTextView.setText("5X5");
            titleTextView.setText("经典模式");
            if ((history == 0 || score < history) && state == Constants.GAME_OVER_NORMAL) {
                historyTextView.setText("新纪录");
            } else {
                historyTextView.setText("历史最佳 " + MyString.timeFormatToString(MyNumber.millisToFormat(history)));
            }
        } else if (mode.equals(Constants.BEST_CLASSIC_HARD_FEW) || mode.equals(Constants.BEST_CLASSIC_HARD_MUCH)) {
            if (state == Constants.GAME_OVER_NORMAL) {
                panel.setBackgroundColor(Constants.BLOCK_GREEN);
                scoreTextView.setText(MyString.timeFormatToString(MyNumber.millisToFormat(score)));
            } else {
                scoreTextView.setText("败了!");
            }
            modeTextView.setText("6X6");
            titleTextView.setText("经典模式");
            if ((history == 0 || score < history) && state == Constants.GAME_OVER_NORMAL) {
                historyTextView.setText("新纪录");
            } else {
                historyTextView.setText("历史最佳 " + MyString.timeFormatToString(MyNumber.millisToFormat(history)));
            }
        } else if (mode.equals(Constants.BEST_ARCADE_EASY)) {
            if (state == Constants.GAME_OVER_NORMAL) {
                panel.setBackgroundColor(Constants.BLOCK_BLACK);
                scoreTextView.setTextColor(Color.WHITE);
                historyTextView.setTextColor(Color.WHITE);
            }
            modeTextView.setText("4X4");
            titleTextView.setText("街机模式");
            scoreTextView.setText(String.valueOf(score));
            if (score > history) {
                historyTextView.setText("新纪录");
            } else {
                historyTextView.setText("历史最佳 " + history);
            }
        } else if (mode.equals(Constants.BEST_ARCADE_NORMAL)) {
            if (state == Constants.GAME_OVER_NORMAL) {
                panel.setBackgroundColor(Constants.BLOCK_BLACK);
                scoreTextView.setTextColor(Color.WHITE);
                historyTextView.setTextColor(Color.WHITE);
            }
            modeTextView.setText("5X5");
            titleTextView.setText("街机模式");
            scoreTextView.setText(String.valueOf(score));
            if (score > history) {
                historyTextView.setText("新纪录");
            } else {
                historyTextView.setText("历史最佳 " + history);
            }
        } else if (mode.equals(Constants.BEST_ARCADE_HARD)) {
            if (state == Constants.GAME_OVER_NORMAL) {
                panel.setBackgroundColor(Constants.BLOCK_BLACK);
                scoreTextView.setTextColor(Color.WHITE);
                historyTextView.setTextColor(Color.WHITE);
            }
            modeTextView.setText("6X6");
            titleTextView.setText("街机模式");
            scoreTextView.setText(String.valueOf(score));
            if (score > history) {
                historyTextView.setText("新纪录");
            } else {
                historyTextView.setText("历史最佳 " + history);
            }
        }

        backButton.setOnTouchListener(onTouchListener);
        resetButton.setOnTouchListener(onTouchListener);
    }

    private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    ((Button)v).setTextSize(36);
                    break;

                case MotionEvent.ACTION_UP:
                    ((Button)v).setTextSize(30);
                    if (v == backButton) {
                        setResult(RESULT_MAIN);
                        finish();
                    } else if (v == resetButton) {
                        setResult(RESULT_RESTART);
                        finish();
                    }
                    break;
            }
            return false;
        }
    };

    @Override
    public void onBackPressed() {
        setResult(RESULT_MAIN);
        finish();
    }
}
